import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type {
  InspectionDataModel,
  InspectionRecordModel,
} from '#/api/equipment/models';

import { ref } from 'vue';

import dayjs from 'dayjs';

import {
  useInspectResultOptions,
  useInspectTypeOptions,
  useQualifiedOptions,
} from '#/api/equipment/models';
import { getLineListApi } from '#/api/master/line';

const rangePresets = ref([
  { label: '最近 7 天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '最近 14 天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '最近 30 天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '最近 90 天', value: [dayjs().add(-90, 'd'), dayjs()] },
]);

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'machineryCode',
      label: '设备编号',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'machineryName',
      label: '设备名称',
      help: '模糊查询',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        labelField: 'lineName',
        valueField: 'id',
        api: getLineListApi,
        immediate: false,
      },
      fieldName: 'lineId',
      label: '生产线',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: useQualifiedOptions(),
      },
      fieldName: 'result',
      label: '点检结果',
    },
    {
      component: 'RangePicker',
      componentProps: {
        presets: rangePresets,
      },
      fieldName: 'rangeDate',
      label: '点检日期',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<InspectionRecordModel>,
): VxeTableGridOptions<InspectionRecordModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'machineryCode', title: '设备编号', width: 160 },
    { field: 'machineryName', title: '设备名称', width: 160 },
    { field: 'workstationCode', title: '工位', width: 100 },
    { field: 'standardPartNo', title: '标件编号', width: 160 },
    {
      field: 'inspectType',
      title: '点检类型',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useInspectTypeOptions(),
      },
    },
    {
      field: 'result',
      title: '点检结果',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useQualifiedOptions(),
      },
    },
    { field: 'inspectionTime', title: '点检时间', width: 150 },
    { field: 'inspector', title: '点检人员', width: 120 },
    { field: 'remark', title: '备注', minWidth: 100 },
    {
      cellRender: {
        attrs: {
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'open',
            text: '数据',
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 80,
    },
  ];
}

/**
 * 获取表格列配置。
 */
export function useDrawerColumns(): VxeTableGridOptions<InspectionDataModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'subjectName', title: '点检项名称', minWidth: 180 },
    { field: 'standard', title: '点检标准', width: 220 },
    { field: 'value', title: '点检值', width: 100 },
    {
      field: 'result',
      title: '点检结果',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useInspectResultOptions(),
      },
    },
    { field: 'remark', title: '备注', minWidth: 100 },
  ];
}
